<template>
    <form-item
        :designer="designer" 
        :field="field" 
        :rules="state.rules"
    >
        <el-input-number 
            v-model="formModel[field.options.name]"
            :size="size"
            :disabled="disabled"
        />
    </form-item>
</template>

<script setup>
import { reactive,onMounted,nextTick,computed } from 'vue';
import FormItem from './form-item.vue';
import {useRules} from '@/hooks/useRules.js'

defineOptions({
    name:"xy-number"
})
const props = defineProps({
    designer:{
        type:Object,
        default:()=>({})
    },
    field:{
        type:Object,
        default:()=>({})
    },
    formModel:{
        type:Object,
        default:()=>({})
    }
})

const state = reactive(({
    fieldVal:0,
    rules:[]
}))

useRules(state,props.field)

const setFieldVal = val=>{
    if(typeof val ==='string') val = Number(val)
    props.formModel[props.field.options.name] = val
}

const disabled = computed(()=>{
    return props.field.options.disabled
})

const size = computed(()=>{
    return props.field.options.size || 'default'
})
onMounted(()=>{
    if(props.field){
        // 存在默认值的情况下进行初始化
        setFieldVal(props.field.options.defaultValue)
    }

    nextTick(()=>{
        if(!props.designer) return
        props.designer.createFieldValFun(props.field.id, setFieldVal)
    })
})
</script>

<style lang="scss" scoped>
</style>
